function getMaterialToCheck() {
    var tbody=window.document.getElementById("material_data");
    jQuery.ajax({
        url:"../../material/site",
        type:"POST",
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    var j = parseInt(i) + 1;
                    str +="<tr>"+"" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">" + j +"</td>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">" + tdata[i].siteName +"</td>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">" + convertTypeToString(tdata[i].type) + "</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">" + "<label onclick='previewMaterial("+ tdata[i].id + "," + tdata[i].type +")'>点击查看素材</label></td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+ tdata[i].startDate.substr(0, 10) + " 至 " + tdata[i].endDate.substr(0, 10) +"</td>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">" + getLaunchTimeByBin(tdata[i].binLaunchTime) + "</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">" + convertMaterialStateToString(tdata[i].state) + "</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">" +
                        "<input type='button' value='通过' id='pass_button'+" + tdata[i].id +" onclick='auditButtonClick( " + tdata[i].id + "," + 0 +")'/>" +
                        "<input type='button' value='拒绝' id='reject_button'+" + tdata[i].id + " onclick='auditButtonClick( " + tdata[i].id + "," + 1 +")'" +"/></span></td></tr>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function convertMaterialStateToString(state) {
    if (state == 0) {
        return "未审核";
    }
    if (state == 1) {
        return "审核成功，待投放";
    }
    if (state == 2) {
        return "审核失败";
    }
    if (state == 4) {
        return "审核成功，投放中";
    }
    if (state == 3) {
        return "审核成功，投放结束";
    }

}

function convertTypeToString(type) {
    if (type == 0) {
        return "图片";
    }
    if (type == 1) {
        return "视频";
    }
    if (type == 2) {
        return "html";
    }
}

function getLaunchTimeByBin(binLaunchTime) {
    var str = "";
    for(var i = 0; i < binLaunchTime.length; i++) {
        if (binLaunchTime.charAt(i) == '1') {
            str = str + i + "时；";
        }
    }
    return str;
}

function showImageOrVideo() {
    var url = window.location.href;
    var id = getArgsFromHref(url, "id");
    var type = getArgsFromHref(url, "type");
    $.ajax({
        url:"../../material/info/" + id,
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code == 0){
                var tdata=data.object;
                if (type == 0) {
                    document.getElementById("_material_video").style.display = "none";
                    document.getElementById("_material_image").setAttribute("src", tdata.url);
                }
                if (type == 1) {
                    document.getElementById("_material_image").style.display = "none";
                    document.getElementById("_material_video").setAttribute("src", tdata.url);
                }
            }
        },
        error:function(){
            alert("search fails！");
        }
    });



}
function previewMaterial(id, type) {
    location.href="./material_preview.html?id="+id+"&type="+type;
}

function getArgsFromHref(sHref, sArgName)
{
    var args = sHref.split("?");
    var retval = "";
    if(args[0] == sHref) /*参数为空*/
    {
        return retval; /*无需做任何处理*/
    }
    var str = args[1];
    args = str.split("&");
    for(var i = 0; i < args.length; i ++)
    {
        str = args[i];
        var arg = str.split("=");
        if(arg.length <= 1) continue;
        if(arg[0] == sArgName) retval = arg[1];
    }
    return retval;
}

function auditButtonClick(id, type) {
    $.ajax({
        url:"../../material/audit",
        type:"POST",
        data:{id:id, type:type},
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                alert("审核完毕");
                location.href="./material_audit.html";
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

}









